Release 10.1A: OpenEdge Development:
Progress 4GL Reference
FOR statement
Starts an iterating block that reads a record from each of one or more tables at the start of each block iteration.
Data movement
![]()
Block properties
Iteration, record reading, record scoping, frame scoping, transactions by default.
Syntax
EACHStarts an iterating block, finding a single record on each iteration. If you do not use the EACH keyword, the Record phrase you use must identify exactly one record in the table.
FIRSTUses the criteria in the
record-phraseto find the first record in the table that meets that criteria. Progress finds the first record before any sorting.
The previous procedure displays customer 1 (cust-num is the primary index of the customer table), not the customer with the lowest credit-limit. A procedure that displays the customer with the lowest credit-limit looks like the following:
See the Notes section for more information on using this option.
LASTUses the criteria in the
record-phraseto find the last record in the table that meets that criteria. Progress finds the last record before sorting.
The procedure above displays the customer with the highest customer number (cust-num is the primary index of the customer table), not the customer with the highest credit-limit.
A procedure that displays the customer with the highest credit-limit looks like the following:
See the Notes section for more information on using this option.
record-phraseIdentifies the set of records you want to retrieve. This can also be the built-in buffer name,
proc-text-buffer, that you can use to return table rows from a stored procedure.To use FOR EACH/FIRST/LAST to access a record in a table defined for multiple databases, you must qualify the record’s table name with the database name.
This is the syntax for
record-phrase:
Specifying multiple occurrences of
record-phraseselects the tables using an inner join.For more information on
record-phraseand inner joins, see the Record phrase reference entry.query-tuning-phraseAllows programmatic control over the execution of a DataServer query. Following is the syntax for the
query-tuning-phrase:
For more information on the
query-tuning-phrase, see OpenEdge Data Management: DataServer for ODBC , OpenEdge Data Management: DataServer for ORACLE , and OpenEdge Data Management: DataServer for Microsoft SQL Server .BREAKOver a series of block iterations, you might want to do some work based on whether the value of a certain field changes. This field defines a break group. For example, you might be accumulating some value, such as a total. You use the BREAK option to define state as the break group. For example:
Here, Progress accumulates the total credit-limit for all the customers in the customer table. Each time the value of the state field changes, Progress displays a subtotal of the credit-limit values for customers in that state.
You can use the BREAK option anywhere in the block header, but you must also use the BY option to name a sort field.
You can use the BREAK option in conjunction with the ACCUMULATE statement and ACCUM function. For more information, see the reference entries for those language elements.
BYexpression[ DESCENDING ]Sorts the selected records by the value of
Note: You cannot reference a BLOB or CLOB field in the BY option.expression. If you do not use the BY option, Progress retrieves records in the order of the index used to satisfy therecord-phrasecriteria, or the primary index if no criteria is given. The DESCENDING option sorts the records in descending order (not in the default ascending order).You can use multiple BY options to do multi-level sorting. For example:
Here, the customers are sorted in order by credit-limit. Within each credit-limit value, customers are sorted alphabetically by name.
There is a performance benefit if an index on expression exists: BREAK BY does not have to perform the sort that is otherwise required to evaluate FIRST, LAST, FIRST-OF, and LAST-OF expressions.
COLLATE (string,strength[ ,collation] ) [ DESCENDING ]Generates the collation value of a string after applying a particular strength, and optionally, a particular collation. The DESCENDING option sorts the records in descending order (not in default ascending order).
stringA CHARACTER expression that evaluates to the string whose collation value you want to generate.
strengthA CHARACTER expression that evaluates to a Progress comparison strength or an International Components for Unicode (ICU) comparison strength.
The Progress comparison strengths include:
RAW — Generates a collation value for the string based on its binary value.
CASE-SENSITIVE — Generates a case-sensitive collation value for the string based on a particular collation. If you specify this strength with an ICU collation, Progress applies the ICU TERTIARY strength.
CASE-INSENSITIVE — Generates a case-insensitive collation value for the string based on a particular collation. If you specify this strength with an ICU collation, Progress applies the ICU SECONDARY strength.
CAPS — Generates a collation value for the string based on its binary value after converting any lowercase letters in the string to uppercase letters, based on the settings of the Internal Code Page (
-cpinternal) and Case Table (-cpcase) startup parameters.The ICU comparison strengths include:
PRIMARY — Generates a collation value for the base characters in the string.
SECONDARY — Generates a collation value for the base characters and any diacritical marks in the string.
TERTIARY — Generates a case-sensitive collation value for the base characters and any diacritical marks in the string.
QUATERNARY — Generates a case-sensitive collation value for the base characters and any diacritical marks in the string, and distinguishes words with and without punctuation. ICU uses this strength to distinguish between Hiragana and Katakana when applied with the ICU-JA (Japanese) collation. Otherwise, it is the same as TERTIARY.
IGNORE-SECONDARY — Generates a case-sensitive, but diacritically-insensitive, collation value for the string.
Note: Use ICU comparison strengths only with ICU collations.collationA CHARACTER expression that evaluates to the name of a Progress collation table or ICU collation. If
collationdoes not appear, COLLATE uses the collation table of the client.Progress reports an error and stops execution if one of the following occurs:
variable=expression1TOexpression2[ BYk]Identifies the name of a field or variable whose value you are incrementing in a loop. The
expression1is the starting value forvariableon the first iteration of the loop. Thekis the amount to add tovariableafter each iteration and must be a constant. It (k) defaults to 1. Thevariable, expression1, andexpression2parameters must be integers.When
variableexceedsexpression2(or is less thanexpression2ifkis negative) the loop ends. Sinceexpression1is compared toexpression2at the start of the first iteration of the block, the block can be executed 0 times. Progress re-evaluatesexpression2on each iteration of the block.WHILEexpressionIndicates the condition in which you want the FOR EACH block to continue processing the statements within it. Using the WHILE
expressionoption causes the block to iterate as long as the condition specified by the expression is TRUE or Progress reaches the end of the index it is scanning, whichever comes first. The expression is any combination of constants, operators, field names, and variable names that yield a logical value.TRANSACTIONIdentifies the FOR EACH block as a system transaction block. Progress starts a system transaction for each iteration of a transaction block if there is not already an active system transaction. See OpenEdge Development: Progress 4GL Handbook for more information on transactions.
on-error-phraseDescribes the processing that takes place when there is an error during a block. This is the syntax for the ON ERROR phrase:
For more information, see the ON ERROR phrase reference entry.
on-endkey-phraseDescribes the processing that takes place when the ENDKEY condition occurs during a block. This is the syntax for the ON ENDKEY phrase:
For more information, see the ON ENDKEY phrase reference entry.
on-quit-phraseDescribes the processing that takes place when a QUIT statement is executed during a block. This is the syntax for the ON QUIT phrase:
For more information, see the ON QUIT phrase reference entry.
on-stop-phraseDescribes the processing that takes place when the STOP conditions occurs during a block. This is the syntax for the ON STOP phrase:
For more information, see the ON STOP phrase reference entry.
frame-phraseSpecifies the overall layout and processing properties of a frame. For more information on
Examplesframe-phrase, see the Frame phrase reference entry.This procedure reads customer records that have a cust-num less than 12, sorting the records in order by state before displaying them:
The next procedure gets information from four related tables (customer, order, order-line, and item) and displays some information from each. Before displaying the information, the FOR EACH statement sorts it in order by the promise-date field, then, within that field, in order by cust-num. Within the cust-num field, the data is sorted by the line-num field.
This procedure uses the LAST option to display information on the last order of each customer:
Notes
1. If one index is unique and all of its components are involved in active equality matches and the other index is not unique, or if not all of its components are involved in active equality matches, Progress chooses the former of the two. 2. Select the index with more active equality matches. 3. Select the index with more active range matches. 4. Select the index with more active sort matches. 5. Select the index that is the primary index. 6. Select the first index alphabetically by index name.
- At compile time, Progress determines which index or indexes to use for retrieving records from a table, based on the conditions in the Record phrase. For compatibility with Progress Version 6 or earlier, you can force Progress to use only one index by specifying the USE-INDEX option or by using the Version 6 Query (-v6q) parameter.
- If you specify the -v6q startup parameter, an index component is involved in an equality match if it is used in the Record phrase conditions in the following form:
Where the
expressionis independent of any fields in the table that the index is being selected from. A condition involving OF and USING are equivalent to this form. A field is involved in a range match if it is used in a condition of this form:Note: The BEGINS operator translates into two range matches for a field.
An equality or range match is considered active if the equality or range condition stands on its own or is related to other conditions solely through the AND operator (for example, not through OR or NOT).
A field is involved in a sort match if it is used in a BY option of this form:
- If you specify the -v6q startup parameter, the following list describes the rules the OpenEdge database manager uses to choose an index for an OpenEdge database:
- If you specify the record by ROWID, Progress accesses the record directly without using an index.
- If you use the USE-INDEX option, in the
record-phrase, Progress uses the index you name in that option.- For each index in the table, Progress looks at each index component in turn and counts the number of active equality, range, and sort matches. Progress ignores the counts for any components of an index that occur after a component that has no active equality match. Progress compares the results of this count and selects the best index. Progress uses the following order to determine the better of any two indexes.
- If you specify the -v6q startup parameter, Progress might have to scan all the records in the index to find those meeting the conditions, or Progress might have to examine only a subset of the records. This latter case is called bracketing the index and results in more efficient access. Having selected an index as previously described, Progress examines each component as follows to see if the index can be bracketed:
- If the component has an active equality match, Progress can bracket it, and it examines the next component for possible bracketing.
- If the component has an active range match, Progress can bracket it, but it does not examine the remaining components for possible bracketing.
- If the component does not have an active equality match or an active range match, Progress does not examine the remaining components for bracketing.
- If you specify the v6q parameter, any conditions you specify in the
record-phrasethat are not involved in bracketing the selected index are applied to the fields in the record itself to determine if the record meets the overallrecord-phrasecriteria. For example, assume that the f table has fields a, b, and c involved in two indexes:
- Primary, unique index (I1) on a, b, and c.
- Secondary non-unique index (I2) on c.
Table 31 shows the index Progress selects and the bracketed part of the index for various
record-phrases.
Table 31: Progress version 6 index selection examples Record phrase Index selected Bracketing on f WHERE a = 3 AND b = 2 AND c = 3 I1 a + b + c f WHERE a = 3 I1 a f WHERE c = 1 I2 c f WHERE a = 3 AND b > 7 AND c = 3 I1 a + b f WHERE a = 3 AND c = 4 I1 a f WHERE b = 5 I1 None of the fields1 f WHERE a = 1 OR b >5 I1 None of the fields1 f WHERE (a >= a1 AND a <= a2)
OR (a1=0) I1 None of the fields2 f WHERE a >= (IF a1 NE 0 THEN a1
ELSE -99999999) AND
a <= (IF a1 NE 0 THEN a2
ELSE +99999999) I1 a2- The FIRST and LAST keywords are especially useful when you are sorting records in a table in which you want to display information. Often, several related records exist in a related table, but you only want to display the first or last related record from that table in the sort. You can use FIRST or LAST in these cases. Two examples follow.
Suppose you were interested in displaying the date when each customer first placed an order. This procedure displays the customer number and date of the first order:
The following procedure displays the last order line of every order, sorted by the price of the item and by the promised date of the order:
- If you want Progress to use a specific index, you must specify the first component of that index in the record phrase of the FOR statement.
- You cannot reference a BLOB or CLOB field in a WHERE clause.
- For more information on the FOR statement, see OpenEdge Development: Progress 4GL Handbook .
- For SpeedScript, the
on-endkey-phraseand theon-quit-phrasedo not apply.See also
FIND statement, Frame phrase, ON ENDKEY phrase, ON ERROR phrase, ON QUIT phrase, ON STOP phrase, Record phrase
|
Copyright © 2005 Progress Software Corporation www.progress.com Voice: (781) 280-4000 Fax: (781) 280-4095 |